<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-card class="search-wrapper">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="医生ID" prop="doctorId">
          <el-input v-model="queryParams.doctorId" placeholder="请输入医生ID" clearable style="width: 200px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">搜索</el-button>
          <el-button @click="resetQuery">重置</el-button>
          <el-button type="success" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 表格区域 -->
    <el-card class="table-wrapper">
      <el-table 
        v-loading="loading" 
        :data="replyList"
        row-key="replyId"
        @sort-change="handleSortChange"
      >
        <el-table-column type="index" label="序号" width="70" />
        <el-table-column label="医生ID" align="center" prop="doctorId" />
        <el-table-column label="回复内容" align="center" prop="content" :show-overflow-tooltip="true" />
        <el-table-column label="排序" align="center" prop="sort" sortable="custom" width="100" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
        <el-table-column label="操作" align="center" width="200">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleEdit(row)">编辑</el-button>
            <el-button type="success" link @click="handleCopy(row)">复制</el-button>
            <el-button type="danger" link @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :total="total"
        :page-sizes="[10, 20, 30, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <!-- 添加/编辑对话框 -->
    <el-dialog 
      :title="dialogType === 'add' ? '新增快捷回复' : '编辑快捷回复'" 
      v-model="dialogVisible" 
      width="500px"
    >
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-form-item label="医生ID" prop="doctorId">
          <el-input v-model="form.doctorId" placeholder="请输入医生ID" />
        </el-form-item>
        <el-form-item label="回复内容" prop="content">
          <el-input
            v-model="form.content"
            type="textarea"
            :rows="4"
            placeholder="请输入回复内容"
            maxlength="500"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="排序号" prop="sort">
          <el-input-number v-model="form.sort" :min="0" :max="999" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, FormInstance } from 'element-plus'
import { listReply, addReply, updateReply, delReply } from '@/api/quick-reply'
import type { QuickReplyData, QuickReplyQuery } from '@/api/quick-reply'

// 查询参数
const queryParams = reactive<QuickReplyQuery>({
  pageNum: 1,
  pageSize: 10,
  doctorId: undefined,
  orderByColumn: 'sort',
  isAsc: 'asc'
})

const loading = ref(false)
const total = ref(0)
const replyList = ref<QuickReplyData[]>([])
const dialogVisible = ref(false)
const dialogType = ref<'add' | 'edit'>('add')
const formRef = ref<FormInstance>()

// 表单校验规则
const rules = {
  doctorId: [{ required: true, message: '请输入医生ID', trigger: 'blur' }],
  content: [{ required: true, message: '请输入回复内容', trigger: 'blur' }],
  sort: [{ required: true, message: '请输入排序号', trigger: 'blur' }]
}

// 表单数据
const form = reactive({
  replyId: undefined as number | undefined,
  doctorId: undefined as number | undefined,
  content: '',
  sort: 0
})

// 获取列表
const getList = async () => {
  loading.value = true
  try {
    const res = await listReply(queryParams)
    if (res.code === 200) {
      replyList.value = res.rows
      total.value = res.total
    }
  } catch (error) {
    console.error(error)
  }
  loading.value = false
}

// 搜索
const handleQuery = () => {
  queryParams.pageNum = 1
  getList()
}

// 重置
const resetQuery = () => {
  queryParams.doctorId = undefined
  handleQuery()
}

// 排序变更
const handleSortChange = (column: any) => {
  queryParams.orderByColumn = column.prop
  queryParams.isAsc = column.order === 'ascending' ? 'asc' : 'desc'
  getList()
}

// 新增
const handleAdd = () => {
  dialogType.value = 'add'
  form.replyId = undefined
  form.doctorId = undefined
  form.content = ''
  form.sort = 0
  dialogVisible.value = true
}

// 编辑
const handleEdit = (row: QuickReplyData) => {
  dialogType.value = 'edit'
  form.replyId = row.replyId
  form.doctorId = row.doctorId
  form.content = row.content
  form.sort = row.sort
  dialogVisible.value = true
}

// 复制内容到剪贴板
const handleCopy = async (row: QuickReplyData) => {
  try {
    await navigator.clipboard.writeText(row.content)
    ElMessage.success('复制成功')
  } catch (error) {
    console.error('复制失败:', error)
    ElMessage.error('复制失败')
  }
}

// 删除
const handleDelete = async (row: QuickReplyData) => {
  try {
    await ElMessage.confirm('是否确认删除该快捷回复？')
    const res = await delReply(row.replyId)
    if (res.code === 200) {
      ElMessage.success('删除成功')
      getList()
    }
  } catch (error) {
    console.error('删除失败:', error)
  }
}

// 提交表单
const submitForm = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate()
  
  try {
    const submitFn = dialogType.value === 'add' ? addReply : updateReply
    const res = await submitFn(form)
    if (res.code === 200) {
      ElMessage.success(dialogType.value === 'add' ? '新增成功' : '修改成功')
      dialogVisible.value = false
      getList()
    }
  } catch (error) {
    console.error('提交失败:', error)
  }
}

// 分页
const handleSizeChange = (val: number) => {
  queryParams.pageSize = val
  getList()
}

const handleCurrentChange = (val: number) => {
  queryParams.pageNum = val
  getList()
}

onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
  
  .search-wrapper {
    margin-bottom: 20px;
  }
  
  .table-wrapper {
    .el-pagination {
      margin-top: 20px;
      justify-content: flex-end;
    }
  }
}
</style> 